package com.yenlien.traceyun.breed.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.breed.entity.BreedCrop;
import com.yenlien.traceyun.breed.entity.BreedCropDetail;
import com.yenlien.traceyun.breed.service.BreedCropService;
import com.yenlien.traceyun.business.entity.Check;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.utils.CheckParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by LiuSha on 2020/9/23.
 */
@Api(tags = "养殖产出物管理")
@RestController
@RequestMapping("/breedCrop")
public class BreedCropController extends BaseController {
    @Autowired
    private BreedCropService breedCropService;

    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    @ApiOperation(value = "列表", notes = "参数示例：{\"page\":\"1\",\"batch\":\"BC20200923\"}")
    public Object findByList(@RequestBody BreedCrop breedCrop) {
        breedCrop.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        return buildPage(breedCropService.findByList(breedCrop));
    }

    @RequestMapping(value = "/findByDetailList", method = RequestMethod.POST)
    @ApiOperation(value = "明细列表", notes = "参数示例：{\"page\":\"1\",\"query\":\"BC20200923\"}")
    public Object findByDetailList(@RequestBody BreedCropDetail breedCropDetail) {
        CheckParams.checkBean(breedCropDetail, new String[]{"breedCropId"});
        return buildPage(breedCropService.findByDetailList(breedCropDetail));
    }

    @RequestMapping(value = "/getBreedDetail", method = RequestMethod.POST)
    @ApiOperation(value = "根据编码类型获取品种信息", notes = "参数示例：{\"type\":\"耳标;RFID;养殖批号\",\"param\":\"参数\"}")
    public Object getBreedDetail(@RequestBody JSONObject param) {
        CheckParams.checkJson(param, new String[]{"type", "param"});
        return success(breedCropService.getBreedDetail(param));
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "新增或修改", notes = "新增或修改时将整个对象传过来，修改时将要修改的整个对象获取到，把需要修改的字段修改就行")
    public Object save(@RequestBody BreedCrop breedCrop) {
        breedCrop.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        breedCropService.save(breedCrop);
        return success("success");
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "查看", notes = "")
    public Object findById(@PathVariable String id) {
        return success(breedCropService.findById(id));
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "删除", notes = "")
    public Object delete(@PathVariable String id) {
        breedCropService.delete(id);
        return success("success");
    }

    @RequestMapping(value = "/deleteDetail/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "根据id删除单条明细")
    public Object deleteDetail(@PathVariable String id) {
        breedCropService.deleteDetail(id);
        return success("success");
    }

}
